package com.yuuko.yuukoapi.repository.system

import com.yuuko.yuukoapi.common.request.PageParam
import com.yuuko.yuukoapi.model.system.Users
import com.yuuko.yuukoapi.model.system.dto.UserPageSpec
import org.babyfish.jimmer.Page
import org.babyfish.jimmer.Specification
import org.babyfish.jimmer.spring.model.SortUtils
import org.babyfish.jimmer.spring.repository.KRepository
import org.babyfish.jimmer.spring.repository.orderBy
import org.babyfish.jimmer.sql.fetcher.Fetcher
import org.springframework.stereotype.Repository

/**
 * <p>
 * UsersRepository 接口
 * </p>
 *
 * @author yuuko
 * @date 2025-06-20
 */
@Repository
interface UsersRepository : KRepository<Users, String> {

    fun findByUsername(username: String): Users?
    fun findPage(page: PageParam, spec: Specification<Users>, fetcher: Fetcher<Users>? = null): Page<Users> {
        return sql.createQuery(Users::class) {
            where(spec)
            orderBy(SortUtils.toSort(page.sort))
            select(table.fetch(fetcher))
        }.fetchPage(page.page, page.pageSize)
    }

}

